xpath($expr); if(!count($result)) { throw new Exception("Login failed"); } } else { $expr = "/api/login[@token]"; $result = $xml->xpath($expr); if(!count($result)) { throw new Exception("Login token not found in XML"); } } return $result[0]->attributes()->token; } function uploadFile ($filename, $token, $filecontent) { global $settings; $url = $settings['wikiroot'] . "/api.php"; $params = array('token' => $token,'filename' => $filename,'action' => 'upload','format' => 'xml','file'=>'@'.$filecontent); $data = httpRequest($url, $params); if (empty($data)) { throw new Exception("No data received from server. Check that API is enabled."); } $xml = simplexml_load_string($data); $expr = "/api/edit[@result='Success']"; $result = $xml->xpath($expr); echo $data; return $result[0]->attributes()->result; } try { global $settings; // perform login incl. confirmation $token = login($settings['user'], $settings['pass']); login($settings['user'], $settings['pass'], $token); //get edit token $url = $settings['wikiroot'] . "/api.php"; $params = "action=query&prop=info&intoken=edit&titles=Main%20Page&format=xml"; $data = httpRequest($url, $params); $xml = simplexml_load_string($data); $expr = "/api/query/pages/page[@edittoken]"; $result = $xml->xpath($expr); //echo ($result[0]->attributes()->edittoken); echo uploadFile (base64_decode($_POST["filename"]), urlencode ($result[0]->attributes()->edittoken), base64_decode("")); } catch (Exception $e) { die("FAILED: " . $e->getMessage()); } ?>